/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.support.alipay.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.yanlight.support.alipay.AlipayConstants;
import com.yanlight.util.EncryptUtil;

/**
 * 
 * @author haolw
 * @time 2017/07/21
 */
public abstract class AlipayUtil {

	/**
	 * 获取用户信息、网站支付宝登录
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 */
	public static void authorizeUser(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl) {
		authorize(request, response, appid, callbackUrl, AlipayConstants.AUTH_USER_SCOPE);
	}

	/**
	 * 用户信息授权
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 */
	public static void authorizeBase(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl) {
		authorize(request, response, appid, callbackUrl, AlipayConstants.AUTH_BASE_SCOPE);
	}

	/**
	 * 商户会员卡
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 */
	public static void authorizeEcard(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl) {
		authorize(request, response, appid, callbackUrl, AlipayConstants.AUTH_ECARD_SCOPE);
	}

	/**
	 * 支付宝闪电开票
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 */
	public static void authorizeInvoiceInfo(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl) {
		authorize(request, response, appid, callbackUrl, AlipayConstants.AUTH_INVOICE_INFO_SCOPE);
	}

	/**
	 * 生活缴费
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 */
	public static void authorizePucCharge(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl) {
		authorize(request, response, appid, callbackUrl, AlipayConstants.AUTH_PUC_CHARGE_SCOPE);
	}

	/**
	 * 用户授权
	 * 
	 * @param request
	 * @param response
	 * @param appid
	 * @param callbackUrl
	 * @param type
	 */
	private static void authorize(HttpServletRequest request, HttpServletResponse response, String appid,
			String callbackUrl, String type) {
		String queryString = request.getQueryString();
		String path = request.getServletPath();
		String redirectUrl = path;
		if (StringUtils.isNotBlank(queryString)) {
			redirectUrl = path.concat("?").concat(queryString);
		}
		redirectUrl = EncryptUtil.encodeBase64Safe(redirectUrl);
		StringBuilder url = new StringBuilder(AlipayConstants.AUTHORIZE_URL);
		url.append("?app_id=" + appid);
		url.append("&scope=" + type);
		url.append("&redirect_uri=");
		url.append(callbackUrl + "?url=");
		url.append(redirectUrl);
		url.append("&state=STATE");
		try {
			response.sendRedirect(url.toString());
		} catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
		return;
	}

}
